package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.CpCountry;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RegistrationInformationMapper;
import com.ruoyi.system.domain.RegistrationInformation;
import com.ruoyi.system.service.IRegistrationInformationService;

/**
 * 报名信息Service业务层处理
 *
 * @author zhangjinlei
 * @date 2022-04-25
 */
@Service
public class RegistrationInformationServiceImpl implements IRegistrationInformationService
{
    @Autowired
    private RegistrationInformationMapper registrationInformationMapper;

    /**
     * 查询报名信息
     *
     * @param id 报名信息主键
     * @return 报名信息
     */
    @Override
    public RegistrationInformation selectRegistrationInformationById(Long id)
    {
        return registrationInformationMapper.selectRegistrationInformationById(id);
    }

    /**
     * 查询选手信息 下拉框
     */
    @Override
    public List<RegistrationInformation>selectPalerList(RegistrationInformation registrationInformation)
    {
        return registrationInformationMapper.selectPalerList(registrationInformation);
    }
    /**
     * 查询报名信息列表
     *
     * @param registrationInformation 报名信息
     * @return 报名信息
     */
    @Override
    public List<RegistrationInformation> selectRegistrationInformationList(RegistrationInformation registrationInformation)
    {
        return registrationInformationMapper.selectRegistrationInformationList(registrationInformation);
    }

    @Override
    public List<RegistrationInformation> selectRegistrationInformationDetailList(RegistrationInformation registrationInformation) {
        return registrationInformationMapper.selectRegistrationInformationDetailList(registrationInformation);
    }


    /**
     * 新增报名信息
     *
     * @param registrationInformation 报名信息
     * @return 结果
     */
    @Override
    public int insertRegistrationInformation(RegistrationInformation registrationInformation) throws Exception {
        List<CpCountry> list = registrationInformationMapper.selectCpCountryList(registrationInformation);
        if(CollectionUtils.isEmpty(list)){
            throw new Exception("本国家没有参加本届运动会或者运动员信息没有录入系统，无法报名");
        }
        registrationInformation.setCountryId(list.get(0).getId());
        registrationInformation.setCreateTime(DateUtils.getNowDate());
        return registrationInformationMapper.insertRegistrationInformation(registrationInformation);
    }

    /**
     * 修改报名信息
     *
     * @param registrationInformation 报名信息
     * @return 结果
     */
    @Override
    public int updateRegistrationInformation(RegistrationInformation registrationInformation)
    {
        registrationInformation.setUpdateTime(DateUtils.getNowDate());
        return registrationInformationMapper.updateRegistrationInformation(registrationInformation);
    }

    /**
     * 批量删除报名信息
     *
     * @param ids 需要删除的报名信息主键
     * @return 结果
     */
    @Override
    public int deleteRegistrationInformationByIds(Long[] ids)
    {
        return registrationInformationMapper.deleteRegistrationInformationByIds(ids);
    }

    /**
     * 删除报名信息信息
     *
     * @param id 报名信息主键
     * @return 结果
     */
    @Override
    public int deleteRegistrationInformationById(Long id)
    {
        return registrationInformationMapper.deleteRegistrationInformationById(id);
    }

    @Override
    public int auditRegistrationInformation(RegistrationInformation registrationInformation){

        registrationInformation.setUpdateTime(DateUtils.getNowDate());
        return registrationInformationMapper.auditRegistrationInformation(registrationInformation);
    }
}
